package com.youdoneed.community.forum.service;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.youdoneed.base.application.AppException;
import com.youdoneed.community.common.utils.StringUUIdUtil;
import com.youdoneed.community.forum.dao.ForummessagelabelMapper;
import com.youdoneed.community.forum.entity.Forummessagelabel;

@Service
public class ForummessagelabelService {
    private final Logger logger = LoggerFactory.getLogger(ForumcommentService.class);
    private static int startUse = 0;
    @Autowired
    ForummessagelabelMapper forummessagelabelMapper;

    @Transactional
    public int deleteForumMessageLabelById(String id) {
        if (!StringUtils.hasText(id)) {
            throw new AppException("id参数不能为空");
        }

        try {
            return forummessagelabelMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            logger.error("根据ID删除失败!");
            return 0;
        }
    }

    @Transactional
    public int saveForumMessageLabel(Forummessagelabel forummessagelabel) {
        if (null == forummessagelabel) {
            throw new AppException("forummessagelabel参数不能为空");
        }
        //统计所有的标签数量
        int count = forummessagelabelMapper.countAllForummessagelabels();
        try {
            forummessagelabel.setForumMessageLabelId(StringUUIdUtil.getUUID());
            forummessagelabel.setCreateDate(new Date());
            forummessagelabel.setLabelStatus(forummessagelabel.getLabelStatus());
            forummessagelabel.setOrderNo(count + 1);
            return forummessagelabelMapper.insertSelective(forummessagelabel);

        } catch (Exception e) {
            logger.error("保存论坛标签异常：" + e);
            return 0;
        }
    }

    @Transactional
    public Forummessagelabel selectForumMessageLabelById(String id) {
        if (!StringUtils.hasText(id)) {
            throw new AppException("id参数不能为空");
        }

        try {
            return forummessagelabelMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            logger.error("查询论坛标签异常：" + e);
            return null;
        }
    }

    @Transactional
    public int updateForumMessageLabel(Forummessagelabel forummessagelabel) {
        if (null == forummessagelabel) {
            throw new AppException("forummessagelabel参数不能为空");
        }
        try {
            return forummessagelabelMapper.updateByPrimaryKeySelective(forummessagelabel);

        } catch (Exception e) {
            logger.error("修改论坛标签异常：" + e);
            return 0;
        }
    }

    @Transactional
    public List<Forummessagelabel> getAllForummessagelabels() {
        logger.info("getAllForummessagelabels");
        try {
            List<Forummessagelabel> listForummessagelabel = forummessagelabelMapper.getAllForummessagelabels();
            return listForummessagelabel;
        } catch (Exception e) {
            logger.error("获取论坛标签异常：" + e);
            return null;
        }
    }

    @Transactional
    public List<Forummessagelabel> getSearcherForummessagelabel(String labelDescribe, Integer labelStatus, String createDateS, String createDateE, Integer currentPage, Integer rows) {
        logger.info("getSearcherForummessagelabel");
        try {
            PageHelper.startPage(currentPage, rows);
            List<Forummessagelabel> listForummessagelabel = forummessagelabelMapper.getSearcherForummessagelabel(labelDescribe, labelStatus, createDateS, createDateE);
            return listForummessagelabel;
        } catch (Exception e) {
            logger.error("获取论坛标签异常：" + e);
            return null;
        }
    }

    @Transactional
    public List<Forummessagelabel> getSearcherForummessagelabelForTotal(String labelDescribe, Integer labelStatus, String createDateS, String createDateE) {
        logger.info("getSearcherForummessagelabel");
        try {
            List<Forummessagelabel> listForummessagelabel = forummessagelabelMapper.getSearcherForummessagelabel(labelDescribe, labelStatus, createDateS, createDateE);
            return listForummessagelabel;
        } catch (Exception e) {
            logger.error("获取论坛标签异常：" + e);
            return null;
        }
    }

    /**
     * 根据帖子id获取对应的标签列表
     *
     * @param forumMsgId
     * @return
     * @author lid
     * @date 2017.6.22
     */
    public List<Forummessagelabel> getLabelByMsgId(String forumMsgId) {
        if (!StringUtils.hasText(forumMsgId)) {
            logger.info("forumMsgId参数为空！");
            throw new AppException("forumMsgId参数为空！");
        }
        logger.info("getLabelByMsgId!forumMsgId:" + forumMsgId);
        try {
            List<Forummessagelabel> listForummessagelabel = forummessagelabelMapper.getLabelByMsgId(forumMsgId);
            ;
            logger.info("listForummessagelabel.size():" + listForummessagelabel.size());
            return listForummessagelabel;
        } catch (Exception e) {
            logger.error("根据帖子id获取对应的标签列表异常：" + e);
            return null;
        }
    }
}
